Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.ReportSource
Imports CrystalDecisions.Shared
Imports Pileso

Public Class frmPedidos_Detalle
    Private mstrCondicion As String
    Private mboLREGISTRO_ALGO As Boolean

    Public WriteOnly Property Condicion() As String

        Set(ByVal value As String)
            mstrCondicion = value
        End Set
    End Property
    Private Sub frmPedidos_Detalle_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        lblStatus.ForeColor = Color.White
        slideStatus.IsOpen = False
        slideStatus.Visible = True

        Me.BackColor = System.Drawing.Color.FromArgb(240, 240, 240)
        lblSubtitulo.ForeColor = System.Drawing.Color.FromArgb(13, 71, 161)
        lblTitulo.ForeColor = Color.White
        dgTerminadas.GridColor = Color.White
       
        txtBuscar.BackColor = System.Drawing.Color.FromArgb(53, 170, 253)
        txtBuscar.Border.BackColor = System.Drawing.Color.FromArgb(53, 170, 253)
        txtBuscar.Border.BackColor2 = System.Drawing.Color.FromArgb(53, 170, 253)
        txtBuscar.Border.BorderColor = System.Drawing.Color.FromArgb(53, 170, 253)
        txtBuscar.Border.BorderColor2 = System.Drawing.Color.FromArgb(53, 170, 253)
        txtBuscar.WatermarkColor = Color.White
        txtBuscar.ForeColor = Color.White

        mboLREGISTRO_ALGO = False

        Try
            Carga_PEDIDOS()
        Catch ex As Exception
            MsgBox(ex.Message)

        End Try

    End Sub
    Sub Carga_PEDIDOS()
        dgTerminadas.Rows.Clear()

        Dim strSql As String
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim m_strFiltro As String = ""

        m_strFiltro = UCase(txtBuscar.Text.Trim)
        m_strFiltro = "%" & m_strFiltro.Replace(" ", "%") & "%"

        strSql = "SELECT P.DETALLE,D.IDDETALLE,P.IDPEDIDO,C.RAZON_SOCIAL,S.DESCRIPCION, NO_PEDIDO,D.FILL_RATE,CEDIS,CONDICIONES,TRANSPORTE,FECHA_INICIO,FECHA_FIN,SEMANA_RECEPCION,SEMANA_FINAL,TIPO_PEDIDO,TIPO_ESTILO,LINEA,UPC,A.CODIGO,A.NOMBRE,D.CANTIDAD_EMPAQUES,D.FACTOR,D.TOTAL_ESTILOS,D.PRECIO_UNITARIO,D.SUBTOTAL,P.NO_DOCUMENTO,P.CITA_CONFIRMACION,P.NO_CONFIRMACION,D.PIEZAS_ENTREGADAS,D.SUBTOTAL_PE,D.PIEZAS_FALTANTES,D.SUBTOTAL_PF,D.FOLIO_GUIA,D.FECHA_ENTEGA_LOG,SEMANA_ENTREGA_LOG,D.FECHA_ENTREGA_CEDIS,D.OBSERVACIONES FROM PEDIDOS P INNER JOIN CTES_PROV C ON P.IDCLIENTE=C.IDPROVEEDOR INNER JOIN SUCURSALES S ON P.IDSUCURSAL=S.IDSUCURSAL INNER JOIN PEDIDOS_DETALLE D ON P.IDPEDIDO=D.IDPEDIDO INNER JOIN ARTICULOS A ON D.IDARTICULO=A.IDARTICULO WHERE P.FILL_RATE<100 AND (RAZON_SOCIAL LIKE '" & m_strFiltro & "' " & mstrCondicion & " OR s.DESCRIPCION LIKE '" & m_strFiltro & "' " & mstrCondicion & " OR CEDIS LIKE '" & m_strFiltro & "' " & mstrCondicion & " OR CONVERT(VARCHAR,NO_PEDIDO) LIKE '" & m_strFiltro & "' " & mstrCondicion & ") ORDER BY FECHA_FIN,C.RAZON_SOCIAL"

        cnConn.Open()
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = strSql

        Dim _Denominacion As Object = (Nothing)
        Dim cont As Integer = 0

        rdBuscar = cmdBuscar.ExecuteReader

        Dim CEDIS As Object
        Dim CONDICIONES As Object
        Dim TRANSPORTE As Object
        Dim LINEA As Object
        Dim UPC As Object
        Dim DETALLES As Object
        Dim DOCUMENTO As Object
        Dim CITA As Object
        Dim CONFIRMACION As Object
        Dim GUIA As Object
        Dim ENTREGA_LOG As Object
        Dim SEMANA_LOG As Object
        Dim ENTREGA_CEDIS As Object
        Dim OBSERVACIONES As Object
        Dim PRECIO As Object
        Dim SUBTOTAL As Object


        Do While rdBuscar.Read()
            If IsDBNull(rdBuscar("CEDIS")) Then
                CEDIS = (Nothing)
            Else
                CEDIS = (rdBuscar("CEDIS"))
            End If

            If IsDBNull(rdBuscar("CONDICIONES")) Then
                CONDICIONES = (Nothing)
            Else
                CONDICIONES = (rdBuscar("CONDICIONES"))
            End If

            If IsDBNull(rdBuscar("TRANSPORTE")) Then
                TRANSPORTE = (Nothing)
            Else
                TRANSPORTE = (rdBuscar("TRANSPORTE"))
            End If

            If IsDBNull(rdBuscar("LINEA")) Then
                LINEA = (Nothing)
            Else
                If rdBuscar("LINEA") = " " Then
                    LINEA = (Nothing)
                Else
                    LINEA = (rdBuscar("LINEA"))
                End If
            End If

            If IsDBNull(rdBuscar("UPC")) Then
                UPC = (Nothing)
            Else
                UPC = (rdBuscar("UPC"))
            End If

            If IsDBNull(rdBuscar("DETALLE")) Then
                DETALLES = (Nothing)
            Else
                DETALLES = ("Ver")
            End If

            If IsDBNull(rdBuscar("NO_DOCUMENTO")) Then
                DOCUMENTO = (Nothing)
            Else
                DOCUMENTO = (rdBuscar("NO_DOCUMENTO"))
            End If

            If IsDBNull(rdBuscar("CITA_CONFIRMACION")) Then
                CITA = (Nothing)
                CONFIRMACION = (Nothing)
            Else
                CITA = (CDate(rdBuscar("CITA_CONFIRMACION")).ToShortDateString)
                If IsDBNull(rdBuscar("NO_CONFIRMACION")) Then
                    CONFIRMACION = ("N/A")
                Else
                    CONFIRMACION = (rdBuscar("NO_CONFIRMACION"))
                End If
            End If




            If IsDBNull(rdBuscar("FOLIO_GUIA")) Then
                GUIA = (Nothing)
            Else
                GUIA = (rdBuscar("FOLIO_GUIA"))
            End If

            If IsDBNull(rdBuscar("FECHA_ENTEGA_LOG")) Then
                ENTREGA_LOG = (Nothing)
            Else
                ENTREGA_LOG = (CDate(rdBuscar("FECHA_ENTEGA_LOG")).ToShortDateString)
            End If

            If IsDBNull(rdBuscar("SEMANA_ENTREGA_LOG")) Then
                SEMANA_LOG = (Nothing)
            Else
                SEMANA_LOG = (rdBuscar("SEMANA_ENTREGA_LOG"))
            End If

            If IsDBNull(rdBuscar("FECHA_ENTREGA_CEDIS")) Then
                ENTREGA_CEDIS = (Nothing)
            Else
                ENTREGA_CEDIS = (CDate(rdBuscar("FECHA_ENTREGA_CEDIS")).ToShortDateString)
            End If

            If IsDBNull(rdBuscar("OBSERVACIONES")) Then
                OBSERVACIONES = (Nothing)
            Else
                OBSERVACIONES = (rdBuscar("OBSERVACIONES"))
            End If

            If IsDBNull(rdBuscar("PRECIO_UNITARIO")) Then
                PRECIO = (0)
            Else
                PRECIO = (rdBuscar("PRECIO_UNITARIO"))
            End If

            If IsDBNull(rdBuscar("SUBTOTAL")) Then
                SUBTOTAL = (0)
            Else
                SUBTOTAL = (rdBuscar("SUBTOTAL"))
            End If


            'Dim ob1() As Object = {}
            cont = cont + 1
            dgTerminadas.Rows.Add(rdBuscar("IDDETALLE"), rdBuscar("IDPEDIDO"), rdBuscar("RAZON_SOCIAL"), rdBuscar("DESCRIPCION"), rdBuscar("NO_PEDIDO"), rdBuscar("FILL_RATE"), CEDIS, CONDICIONES, TRANSPORTE, CDate(rdBuscar("FECHA_INICIO")).ToShortDateString, CDate(rdBuscar("FECHA_FIN")).ToShortDateString, rdBuscar("SEMANA_RECEPCION"), rdBuscar("SEMANA_FINAL"), rdBuscar("TIPO_PEDIDO"), rdBuscar("TIPO_ESTILO"), LINEA, UPC, rdBuscar("CODIGO"), rdBuscar("NOMBRE"), rdBuscar("CANTIDAD_EMPAQUES"), rdBuscar("FACTOR"), rdBuscar("TOTAL_ESTILOS"), PRECIO, SUBTOTAL, DOCUMENTO, CITA, CONDICIONES, rdBuscar("PIEZAS_ENTREGADAS"), rdBuscar("SUBTOTAL_PE"), rdBuscar("PIEZAS_FALTANTES"), rdBuscar("SUBTOTAL_PF"), GUIA, ENTREGA_LOG, SEMANA_LOG, ENTREGA_CEDIS, OBSERVACIONES)
        Loop

        cnConn.Close()

    End Sub

    Private Sub btnReporte_Click(sender As System.Object, e As System.EventArgs) Handles btnReporte.Click
        Dim FECHA_ACTUAL As String = Now.ToString("MM/dd/yyyy")
        Call SetSqlCitra("UPDATE PEDIDOS SET REPORTEAR=0")
        Call SetSqlCitra("UPDATE PEDIDOS SET REPORTEAR=1 WHERE CONVERT(VARCHAR,FECHA_RECEPCION,101)='" & FECHA_ACTUAL & "'")

        Dim rpt As New crPEDIDOS.CrystalReport1
        Dim VisorInforme As New frmReportePedidoDiario
        VisorInforme.CrystalReportViewer1.ReportSource = rpt
        VisorInforme.ShowDialog()


    End Sub

    Private Sub txtBuscar_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtBuscar.TextChanged
        Carga_PEDIDOS()

    End Sub

    Private Sub btnMM_Click(sender As System.Object, e As System.EventArgs) Handles btnMM.Click
        If WindowState = FormWindowState.Normal Then
            WindowState = FormWindowState.Maximized
        ElseIf WindowState = FormWindowState.Maximized Then
            WindowState = FormWindowState.Normal
        End If
    End Sub

    Private Sub btnRegresar_Click(sender As System.Object, e As System.EventArgs) Handles btnRegresar.Click
        If mboLREGISTRO_ALGO = False Then
            DialogResult = Windows.Forms.DialogResult.None
        Else
            DialogResult = Windows.Forms.DialogResult.OK

        End If
        Close()
    End Sub

    Private Sub btnReporteHEP_Click(sender As System.Object, e As System.EventArgs) Handles btnReporteHEP.Click
        Dim FECHA_ACTUAL As String = Now.ToString("MM/dd/yyyy")
        Call SetSqlCitra("UPDATE PEDIDOS SET REPORTEAR=0")
        Call SetSqlCitra("UPDATE PEDIDOS SET REPORTEAR=1 WHERE CONVERT(VARCHAR,FECHA_RECEPCION,101)='" & FECHA_ACTUAL & "'")

        ' Dim rpt As New crPEDIDOS.CrystalReport1
        Dim VisorInforme As New frmReportePedidoDiario
        'VisorInforme.CrystalReportViewer1.ReportSource = rpt
        VisorInforme.ShowDialog()
    End Sub
End Class